package org.yak.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 字符串处理工具类
 * @author: jiangbin
 * @date: 2011-6-6上午10:28:09
 */
public class StringEx {

	/**
	 * 字符串首字母小写
	 *
	 * @param str
	 * @return
	 */
	public static String toLowerCaseFirstOne(String str) {
		if (str == null || "".equals(str))
			return str;
		if (Character.isLowerCase(str.charAt(0)))
			return str;
		else
			return (new StringBuilder())
					.append(Character.toLowerCase(str.charAt(0)))
					.append(str.substring(1)).toString();
	}

	/**
	 * 字符串首字母大写
	 *
	 * @param str
	 * @return
	 */
	public static String toUpperCaseFirstOne(String str) {
		if (str == null || "".equals(str))
			return str;
		if (Character.isUpperCase(str.charAt(0)))
			return str;
		else
			return (new StringBuilder())
					.append(Character.toUpperCase(str.charAt(0)))
					.append(str.substring(1)).toString();
	}

	/**
	 * 转换对象为String类型
	 *
	 * @param Object
	 *            需要转换的对象
	 * @author px
	 * @return String
	 */
	public static String parseString(Object obj) {
		try {
			if (obj != null) {
				return obj.toString();
			} else {
				return "";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 转换对象为int类型
	 *
	 * @param Object
	 *            需要转换的对象
	 * @author px
	 * @return
	 */
	public static int parseInt(Object in) {
		String ins = parseString(in);
		try {
			return in != null && !"".equals(ins) ? Integer.parseInt(ins) : 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 转换对象为double类型
	 *
	 * @param Object
	 *            需要转换的对象
	 * @author px
	 * @return double
	 */
	public static double parseDouble(Object in) {
		String ins = parseString(in);
		try {
			return in != null && !"".equals(ins) ? Double.parseDouble(ins) : 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 0d;
		}
	}

	/**
	 * 转换对象为long类型
	 *
	 * @param Object
	 *            需要转换的对象
	 * @author px
	 * @return long
	 */
	public static long parseLong(Object in) {
		String ins = parseString(in);
		try {
			return in != null && !"".equals(ins) ? Long.parseLong(ins) : 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 0l;
		}
	}

	public static boolean parseBoolean(Object in) {
		String ins = parseString(in);
		try {
			if(in != null && !"".equals(ins)){
				return Boolean.parseBoolean(ins);
			}
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 将传入的数字转换成以逗号分隔的形式，如传入123456789，转换成123,456,789
	 *
	 * @author px
	 * @param number
	 * @return
	 */
	public static String getShowNumber(long number) {
		// 如果是负数，取正
		boolean bellowzero = false;
		if (number < 0) {
			bellowzero = true;
			number = -number;
		}
		StringBuilder sb = new StringBuilder(number + "");
		// 根据数字的大小决定循环几次
		int count = (sb.length() - 1) / 3;
		int mod = sb.length() % 3 == 0 ? 3 : sb.length() % 3;
		for (int i = 1; i <= count; i++) {
			sb.insert(mod + 3 * (count - i), ",");
		}
		if (bellowzero) {
			sb.insert(0, "-");
		}
		return sb.toString();
	}
	
	/**
	 * 匹配查询字符串中以某字符开始并且以某字符结束的中间内容
	 * 例如：{databaseIp}:{databasePort}{instanceName};database 查询出以{}包含的内容字符串并且返回list集合
	 * @param regText
	 * @param indexOfValue
	 * @param endOfValue
	 * @return
	 */
	public static List<String> parseMatcherList(String regText,String indexOfValue,String endOfValue){
		List<String> ls=new ArrayList<String>();
		//替换匹配正值表达式
		Pattern pattern = Pattern.compile("(?<=\\"+indexOfValue+")(.+?)(?=\\"+endOfValue+")");
		Matcher matcher = pattern.matcher(regText);
		while(matcher.find()){
			ls.add(matcher.group());
		}
		return ls;
	}

}
